package com.shanshan.manage.web.controller;

import com.shanshan.manage.common.annotation.Log;
import com.shanshan.manage.common.core.controller.BaseController;
import com.shanshan.manage.common.core.domain.AjaxResult;
import com.shanshan.manage.common.core.page.TableDataInfo;
import com.shanshan.manage.common.enums.BusinessType;
import com.shanshan.manage.common.utils.poi.ExcelUtil;
import com.shanshan.manage.web.domain.AGoods;
import com.shanshan.manage.web.service.IAGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 商品Controller
 *
 * @author zhaoShuHeng
 * @date 2023-08-30
 */
@RestController
@RequestMapping("/aGoods/aGoods")
public class AGoodsController extends BaseController {
    @Autowired
    private IAGoodsService aGoodsService;

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('aGoods:aGoods:list')")
    @GetMapping("/list")
    public TableDataInfo list(AGoods aGoods) {
        startPage();
        List<AGoods> list = aGoodsService.selectAGoodsList(aGoods);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('aGuest:aGuest:list')")
    @GetMapping("/listAGoodsNoPage")
    public TableDataInfo listAGoodsNoPage(AGoods aGoods) {
        List<AGoods> list = aGoodsService.selectAGoodsList(aGoods);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('aGoods:aGoods:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AGoods aGoods) {
        List<AGoods> list = aGoodsService.selectAGoodsList(aGoods);
        ExcelUtil<AGoods> util = new ExcelUtil<AGoods>(AGoods.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('aGoods:aGoods:query')")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId) {
        return success(aGoodsService.selectAGoodsByGoodsId(goodsId));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('aGoods:aGoods:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AGoods aGoods) {
        BigDecimal allPrice = aGoods.getAllPrice();
        BigDecimal bigDecimal = new BigDecimal(aGoods.getGoodsNum());
        BigDecimal divide = allPrice.divide(bigDecimal,3, RoundingMode.HALF_UP);
        aGoods.setPrice(divide);
        return toAjax(aGoodsService.insertAGoods(aGoods));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('aGoods:aGoods:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AGoods aGoods) {
        BigDecimal allPrice = aGoods.getAllPrice();
        BigDecimal bigDecimal = new BigDecimal(aGoods.getGoodsNum());
        BigDecimal divide = allPrice.divide(bigDecimal,3, RoundingMode.HALF_UP);
        aGoods.setPrice(divide);
        return toAjax(aGoodsService.updateAGoods(aGoods));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('aGoods:aGoods:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds) {
        return toAjax(aGoodsService.deleteAGoodsByGoodsIds(goodsIds));
    }
}
